// pages/insurance/insurance.js
const utils = require('../../utils/util.js')

Page({
  data: {
    // 五险一金设置
    insuranceEnabled: false,
    insuranceBase: '',
    insuranceItems: {
      pension: { name: '养老保险', companyRate: 16, personalRate: 8, enabled: true },
      medical: { name: '医疗保险', companyRate: 8, personalRate: 2, enabled: true },
      unemployment: { name: '失业保险', companyRate: 0.7, personalRate: 0.3, enabled: true },
      injury: { name: '工伤保险', companyRate: 0.5, personalRate: 0, enabled: true },
      maternity: { name: '生育保险', companyRate: 0.8, personalRate: 0, enabled: true },
      housingFund: { name: '住房公积金', companyRate: 12, personalRate: 12, enabled: true }
    },
    
    // 状态控制
    saving: false,
    loading: false
  },

  /**
   * 页面加载时初始化
   */
  onLoad: function (options) {
    this.loadSettings()
  },

  /**
   * 页面显示时重新加载设置
   */
  onShow: function () {
    this.loadSettings()
  },

  /**
   * 加载设置到表单
   */
  loadSettings: function () {
    try {
      const settings = utils.getStoredSettings()
      
      this.setData({
        insuranceEnabled: settings.insurance.enabled,
        insuranceBase: settings.insurance.base > 0 ? settings.insurance.base.toFixed(2) : '',
        insuranceItems: settings.insurance.items
      })
      
    } catch (error) {
      console.error('加载设置失败:', error)
      utils.showToast('加载设置失败', 'error')
    }
  },

  /**
   * 切换五险一金启用状态
   */
  toggleInsurance: function () {
    this.setData({
      insuranceEnabled: !this.data.insuranceEnabled
    })
  },

  /**
   * 五险一金基数输入处理
   */
  onInsuranceBaseInput: function (e) {
    const value = e.detail.value
    // 允许用户清空输入框
    this.setData({
      insuranceBase: value
    })
  },

  /**
   * 更新保险项目比例
   */
  updateInsuranceRate: function (e) {
    const { type, field } = e.currentTarget.dataset
    const value = e.detail.value
    
    this.setData({
      [`insuranceItems.${type}.${field}Rate`]: value
    })
  },

  /**
   * 切换保险项目启用状态
   */
  toggleInsuranceItem: function (e) {
    const { type } = e.currentTarget.dataset
    this.setData({
      [`insuranceItems.${type}.enabled`]: !this.data.insuranceItems[type].enabled
    })
  },

  /**
   * 使用工资作为基数
   */
  useSalaryAsBase: function () {
    // 从全局设置中获取月薪
    const settings = utils.getStoredSettings()
    const monthlySalary = settings.monthlySalary
    
    if (monthlySalary > 0) {
      this.setData({
        insuranceBase: monthlySalary.toFixed(2)
      })
      utils.showToast('已使用月薪作为基数')
    } else {
      utils.showToast('请先设置月薪', 'error')
    }
  },

  /**
   * 保存设置
   */
  saveSettings: function (e) {
    this.setData({ saving: true })

    // 获取当前全局设置
    const currentSettings = utils.getStoredSettings()
    
    // 更新保险设置
    const insuranceSettings = {
      enabled: this.data.insuranceEnabled,
      base: parseFloat(this.data.insuranceBase) || 0,
      items: this.data.insuranceItems
    }

    // 合并设置
    const newSettings = Object.assign({}, currentSettings, {
      insurance: insuranceSettings
    })

    setTimeout(() => {
      try {
        utils.saveSettings(newSettings)
        
        this.setData({ saving: false })
        utils.showToast('五险一金设置已保存')
        
      } catch (error) {
        console.error('保存设置失败:', error)
        this.setData({ saving: false })
        utils.showToast('保存失败，请重试', 'error')
      }
    }, 500)
  }
})